import type { Slide } from '@/types/slides'
import { formatTime } from '@/utils/timer'
import { GetTemplateItemData } from './checkTemplate'
import { nanoid } from 'nanoid'

export async function GetWelcomeCourseware(tempId: string, params: PPTWelcomeDataSchema): Promise<Slide | undefined> {
  const tempData = await GetTemplateItemData(tempId, 'welcome')
  if (!tempData) return

  let strSlide = JSON.stringify(tempData.default)
  
  const { title, author } = params
  const now = formatTime('YYYY-MM-DD', new Date())

  strSlide = strSlide.replace('%幻灯片标题%', title) // 标题
  strSlide = strSlide.replace('%幻灯片演讲人%', author) // 演讲人
  strSlide = strSlide.replace('%幻灯片时间%', now) // 演讲时间

  const slideData = JSON.parse(strSlide)

  slideData.id = nanoid(10)
  slideData.elements.forEach((el: any) => {
    el.id = nanoid(10)
  })
  
  return slideData
}
